﻿using System;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.WEB.GIMS.Class;
using KPIS.GERP.WEB.GIMS.MasResourceService;
using Resources;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class MasResourceTypeController : Controller
    {
        MasResourceServiceClient s = new MasResourceServiceClient();

        #region resource type

        public ActionResult Index()
        {
            // Resource type
            var model = s.SelectResourceType(new MasResourceType{RecordStatus = "!D"}, null);
            return View(model); 
        }

		public ActionResult Details(int id)
		{
			var model = s.SelectResourceType(new MasResourceType {ResTypeId = id}, null).FirstOrDefault();
			return View(model);
		}

		public ActionResult Create()
		{
		    var model = new MasResourceType();
			return View(model);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Create(MasResourceType model)
		{
		    model = VarifyModel(model);
			if (ModelState.IsValid)
			{
				var id = s.InsertResourceType(model, GimsMembership.CurrentEmpId);
				return RedirectToAction("Details", new {id});
			}
			return View(model);
		}

		public ActionResult Edit(int id)
		{
			var model = s.SelectResourceType(new MasResourceType {ResTypeId = id}, null).FirstOrDefault();
			return View(model);
		}

		[HttpPost, ValidateAntiForgeryToken]
		public ActionResult Edit(int id, MasResourceType model)
		{
		    model = VarifyModel(model);
			if (ModelState.IsValid)
			{
				s.UpdateResourceType(model, GimsMembership.CurrentEmpId);
				return RedirectToAction("Details", new {id});
			}

			return View(model);
		}

		public ActionResult Delete(int id)
		{
			try
			{
				s.DeleteResourceType(id, GimsMembership.CurrentEmpId);
				return RedirectToAction("Index");
			}
			catch (Exception ex)
			{
			    ViewBag.ErrorMessage = ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message;
				Models.Class.GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
			}
            return RedirectToAction("Index");
		}
		
		[HttpPost, ValidateAntiForgeryToken]
		public ActionResult DeleteAll(int[] deleteIds)
		{
			if (deleteIds == null)
				return Json(new { success = true });

			try
			{
				foreach (int delete in deleteIds)
				{
					s.DeleteResourceType(delete, GimsMembership.CurrentEmpId);
				}

				ModelState.AddModelError(ErrorLocalization.Error, string.Format("ลบข้อมูลไปทั้งหมด {0} รายการ", deleteIds.Length.ToString("#,##0")));
				return Json(new {success = true});
			}
			catch (Exception ex)
			{
				ModelState.AddModelError(ErrorLocalization.Error, ErrorLocalization.ErrorWhileDeleteData);
				Models.Class.GerpLog.Error(ErrorLocalization.ErrorWhileDeleteData + ", " + ex.Message);
				return Json(new {success = false});
			}
		}
        
        private MasResourceType VarifyModel(MasResourceType model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
            {
                ModelState.AddModelError("Name", "กรุณากรอกชื่อหมวด");
            }

            return model;
        }
#endregion

    }
}
